﻿var viewLangugaModel = new function () {

    this.Languages = ko.observableArray(data.ListLanguage);
    this.CurrentPageIndex = ko.observable(1);
    this.PageSize = ko.observable(10);
    this.PageText = ko.computed(function () {
        var total = this.Languages().length;
        if (total == 1)
            return total + " ngôn ngữ";
        else
            return total + " ngôn ngữ";

    }, this);

    this.SetOrder = SetOrder;
    this.colName = ko.observable("Title");
    this.sortOrder = ko.observable("asc");

    //Insert max page
    this.maxPageIndex = ko.computed(function () {
        if (this.PageSize() == 0) {
            this.PageSize = ko.observable(10);
        }
        return Math.ceil(ko.utils.unwrapObservable(this.Languages()).length / this.PageSize());
    }, this);

    this.PrevButton = function () {
        this.CurrentPageIndex(this.CurrentPageIndex() - 1);
    };

    this.PrevButtonClass = ko.computed(function () {
        var page = parseInt(this.CurrentPageIndex());
        if (page > 1) {
            return "btn btn-prev-arrow prev-active";
        }
        else {
            return "btn btn-prev-arrow";
        }
    }, this);

    this.NextButtonClass = ko.computed(function () {
        if (this.CurrentPageIndex() == this.maxPageIndex() || this.maxPageIndex() == 1)
            return "btn btn-next-arrow next-disabled";
        else
            return "btn btn-next-arrow";
    }, this);

    this.NextButton = function () {
        this.CurrentPageIndex(this.CurrentPageIndex() + 1);
    };

    this.ItemsOnPage = ko.computed(function () {
        var currentPage = this.CurrentPageIndex();
        var maxPage = this.maxPageIndex();

        if (parseInt(currentPage) == 0)
            this.CurrentPageIndex(1);

        if (parseInt(currentPage) >= parseInt(maxPage)) {
            this.CurrentPageIndex(parseInt(maxPage));
        }
        if (parseInt(this.PageSize()) <= 0) {
            this.PageSize(10);
        }
        var startIndex = this.PageSize() * (this.CurrentPageIndex() - 1);
        var endIndex = parseInt(startIndex) + parseInt(this.PageSize());

        return this.Languages().slice(startIndex, endIndex);
    }, this);


    // Sort 
    function SetOrder(event, colName) {
        if (viewPartnerModel.colName() === colName) {
            switchSortOrder();
        }
        else {
            viewPartnerModel.colName(colName);
            viewPartnerModel.sortOrder("desc");
        }
        SetTHClass(event, colName);
        if (colName != "")
            this.Partners(SortByPropertyName(ko.utils.unwrapObservable(viewPartnerModel.Partners()), colName, viewPartnerModel.sortOrder()));
    }
    this.SetOrder = SetOrder;

    function SetTHClass(event, colName) {
        //remove the existing classes
        $("div.title").parent().find(".sort_wrapper").children("span").removeClass("asc");
        $("div.title").parent().find(".sort_wrapper").children("span").removeClass("desc");

        if (viewPartnerModel.sortOrder() === "asc") {
            $("#" + colName + " span").addClass("asc");

        } else {
            $("#" + colName + " span").addClass("desc");
        }
    }

    function switchSortOrder() {
        if (viewPartnerModel.sortOrder() === "asc") {
            viewPartnerModel.sortOrder("desc");
        } else {
            viewPartnerModel.sortOrder("asc");
        }
    }

    // sort all items
    function SortByPropertyName(items, propertyName, sortOrder) {
        return items.sort(sortProperty(propertyName, sortOrder));
    }
    function sortProperty(property, sortOrder) {
        if (sortOrder == "asc")
            return function (a, b) {
                if (a[property] != null && b[property] != null) {
                    var number = parseFloat(a[property].toLowerCase());
                    if (number != NaN && number > 0)
                        return (parseFloat(a[property].toLowerCase()) < parseFloat(b[property].toLowerCase())) ? -1 : (parseFloat(a[property].toLowerCase()) > parseFloat(b[property].toLowerCase())) ? 1 : 0;
                    else
                        return (a[property].toLowerCase() < b[property].toLowerCase()) ? -1 : (a[property].toLowerCase() > b[property].toLowerCase()) ? 1 : 0;
                }
            }
        else
            return function (a, b) {
                if (a[property] != null && b[property] != null) {
                    var number = parseFloat(a[property].toLowerCase());
                    if (number > 0)
                        return (parseFloat(a[property].toLowerCase()) > parseFloat(b[property].toLowerCase())) ? -1 : (parseFloat(a[property].toLowerCase()) < parseFloat(b[property].toLowerCase())) ? 1 : 0;
                    else
                        return (a[property].toLowerCase() > b[property].toLowerCase()) ? -1 : (a[property].toLowerCase() < b[property].toLowerCase()) ? 1 : 0;
                }
            }
    }

    // Delete Emails
    this.DeleteLanguage = function () {
        var languageId = $('#languageId').val();

        var url = "/Management/LanguageList/Delete?id=" + languageId;

        // Append message waiting
        closePopup();
        openProcess();

        // Json Get function to retrive data from server
        $.getJSON(url, function (data) {
            if (data != null && data != undefined) {

                viewLangugaModel.Languages(data.ListLanguage);

                // Close message waiting
                hideProcess();
            }
        });
    };
}

ko.applyBindings(viewLangugaModel, document.getElementById("main"));

function DeleteDialog(id) {
    $('#languageId').val(id);
    $('#title').text($('#language' + id).attr("rel"));

    $('#fancybox-overlay').show();
    $('#cfmDelete').showPopup({
        width: 380
    });
}